package com.sl.springboot.controller;

import com.sl.springboot.model.Coach;
import com.sl.springboot.model.User;
import com.sl.springboot.model.PageInfo;
import com.sl.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class UserController {

    // 依赖注入
    @Autowired
    private UserService userService;


    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findUser")
    public String findUser(Integer pageIndex, Integer pageSize,String username,String phone, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("username",username);
        mp.put("phone",phone);
        PageInfo<User> pageList = userService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "UserList";
    }

    /**
     * 获取我的会员
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findMyUser")
    public String findMyUser(Integer pageIndex, Integer pageSize,String username,String phone, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        String type = (String)session.getAttribute("type");
        Map mp = new HashMap();
        mp.put("username",username);
        mp.put("phone",phone);
        if(type.equals("03")){
            Coach coach = (Coach) session.getAttribute("ad");
            mp.put("instructor",coach.getUsername());
        }

        PageInfo<User> pageList = userService.findMyUser(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "UserList";
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/addUser" ,method = RequestMethod.POST)
    @ResponseBody
    public String addUser( @RequestBody User user) {
        List<User> all = userService.getAll();
        for(int i=0;i<all.size();i++){
            if(user.getUsername().equals(all.get(i).getUsername())){
                return "201";
            }
        }
        int d = userService.addUser(user);
        return "200";
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteUser")
    @ResponseBody
    public String deleteUser(String id) {
        int d = userService.deleteUser(id);
        return "UserList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateUser")
    public String updateUser( User user) {
        int d = userService.updateUser(user);
        return "redirect:/findUser";
    }


    /**
     * 按照ID查询
     */
    @RequestMapping( "/findUserById")
    public String findUserById(String id,Model model,HttpServletRequest request) {
        User user= userService.findUserById(id);
        model.addAttribute("user",user);
        return "UserEdit";
    }


}
